package com.chenenjun.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chenenjun.service.ArticleServiceImpl;
import com.chenenjun.service.IArticleService;
import com.chenenjun.vo.Article;
import com.chenenjun.vo.BBSUser;
import com.chenenjun.vo.PageBean;

/**
 * Servlet implementation class ArticleController
 */
@WebServlet(name="/ArticleController", urlPatterns={"/article"})
public class ArticleController extends HttpServlet {
	
	private IArticleService service = new ArticleServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		RequestDispatcher dispatcher = null;
		switch (action) {
		case "query":{
			String curpage = request.getParameter("curpage");
//			List<Article> list = service.queryArticles();
			int userid = 999;
			BBSUser user = (BBSUser)request.getSession().getAttribute("user");
			if(user != null){
				userid = user.getId();
			}
			PageBean pageBean = service.queryAByP(Integer.parseInt(curpage), userid);
			request.setAttribute("pageBean", pageBean);
			dispatcher = request.getRequestDispatcher("/show.jsp");
			break;
		}
		case "delete":{
			String id = request.getParameter("id");
			boolean flag = service.delArticle(Integer.parseInt(id), 0);
			if(flag){
				dispatcher = request.getRequestDispatcher("article?action=query&curpage=1");
			}
			break;
		}
		case "deletec":{
			String id = request.getParameter("id");
			String rootid = request.getParameter("rootid");
			boolean flag = service.delArticle(Integer.parseInt(id), Integer.parseInt(rootid));
			if(flag){
				dispatcher = request.getRequestDispatcher("article?action=queryc&id="+rootid);
			}
			break;
		}
		case "add":{
			String rootid = request.getParameter("rootid");
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			Article a = new Article();
			a.setRootid(Integer.parseInt(rootid));
			a.setTitle(title);
			a.setContent(content);
			a.setUser((BBSUser) request.getSession().getAttribute("user"));
			if(service.addArticle(a)){
				dispatcher = request.getRequestDispatcher("article?action=query&curpage=1");
			}
			break;
		}
		case "reByAJAX":{// 通过ajax技术进行回帖
			String rootid = request.getParameter("rootid");
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			Article a = new Article();
			a.setRootid(Integer.parseInt(rootid));
			a.setTitle(title);
			a.setContent(content);
			a.setUser((BBSUser) request.getSession().getAttribute("user"));
			if(service.addArticle(a)){
				dispatcher = request.getRequestDispatcher("article?action=queryc&id="+rootid);
			}
			break;
		}
		case "queryc":{
			String id = request.getParameter("id");// 主贴ID
			String json = service.queryArticlesById(Integer.parseInt(id));
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
			break;
		}
		default:
			break;
		}
		if(dispatcher != null){
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

